package com.bwie.controller;

import com.bwie.pojo.DriverLocation;
import com.bwie.service.impl.DriverGeoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.geo.GeoResult;

@RestController
@RequestMapping("/server/index")
public class DriverController {

    @Autowired
    private DriverGeoService driverGeoService;

    @PostMapping("/add")
    public String addDriver(@RequestParam String driverId,
                            @RequestParam double longitude,
                            @RequestParam double latitude) {
        driverGeoService.addDriverLocation(new DriverLocation(driverId, longitude, latitude));
        return "OK";
    }

    @GetMapping("/nearby")
    public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> findNearbyDrivers(
            @RequestParam double longitude,
            @RequestParam double latitude,
            @RequestParam double radiusKm,
            @RequestParam(defaultValue = "5") long count) {
        return driverGeoService.findNearbyDrivers(longitude, latitude, radiusKm, count);
    }

    @GetMapping("/distance")
    public double getDistance(@RequestParam String driver1,
                              @RequestParam String driver2) {
        return driverGeoService.getDistance(driver1, driver2);
    }
}